﻿<?php
error_reporting(E_ALL);
ini_set("display_errors","On");
//就近 www.jiujin.me
if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;
$id = $_GPC['id'];
//$qid= $_GPC['qid'];
echo($id);
die;

$openid = m('user')->getOpenid();
$member = m('member')->getInfo($openid);
//gmm 2015-12-16 扫码领余额
$time = time();
$saoyue = pdo_fetch("SELECT * FROM " . tablename('ewei_shop_saoyue') . " WHERE id = '{$id}' and uniacid = '{$_W['uniacid']}' and status = 1 and 
	'{$time}' > starttime and '{$time}' < endtime ");

if(!empty($saoyue)){
	$saoyue_limit = intval($saoyue['limit']);
	$saoyue_perlimit = intval($saoyue['perlimit']);
	//扫过的次数
	$all_count = pdo_fetch("SELECT count(*) as count FROM " . tablename('ewei_shop_saoyue_record') . " WHERE saoyue = '{$id}' and uniacid = '{$_W['uniacid']}'");
	if(intval($all_count['count']) < $saoyue_limit){
		$count = pdo_fetch("SELECT count(*) as count FROM " . tablename('ewei_shop_saoyue_record') . " WHERE saoyue = '{$id}' and uniacid = '{$_W['uniacid']}' and  uid = '{$member['uid']}'");
		if(intval($count['count']) < $saoyue_perlimit || $saoyue_perlimit == 0 || $saoyue_perlimit == null){
			
			$pid = pdo_fetch("SELECT id,pid as count FROM " . tablename('ewei_shop_saoyue_record') . " WHERE qid='{$qid}' saoyue = '{$id}' and uniacid = '{$_W['uniacid']}' and  uid = '{$member['uid']}'");
			
			if(intval($pid['count'])=0){
			
			$saoyue_ok = 1;
			$saoyue_rest = $saoyue_limit - intval($count['count']) - 1;
			$insert = array(
				'uniacid' => $_W['uniacid'],
				'saoyue' => $id,
				'num' => $saoyue['num'],
				'createtime' => time(),
				'uid' => $member['uid'],
				'nickname' => $member['nickname']
			);
                        $logno = m('common')->createNO('member_log', 'logno', 'RC');
                        $data = array('openid' =>$openid, 'logno' => $logno, 'uniacid' => $_W['uniacid'], 'type' => '0', 'createtime' => time(), 'status' => '1', 'title' =>"扫描领余额", 'money' =>$saoyue['num'], 'rechargetype' => 'saoyue');
		        pdo_insert('ewei_shop_member_log', $data);
			pdo_insert('ewei_shop_saoyue_record', $insert);
			//修改余额
			$credit2 = intval($member['credit2']) + intval($saoyue['num']);
			$data = array('credit2' => $qid);
			pdo_update('mc_members', $data, array('uniacid' => $_W['uniacid'], 'uid' => $member['uid']));
			pdo_update('ewei_shop_saoyue_record', array('qid' => $credit2), array('id' => $pid['id'],'uniacid' => $_W['uniacid'], 'uid' => $member['uid']));
			}else{
			$saoyue_ok = 0;
		}
			
		}else{
			$saoyue_ok = 0;
		}
	}else{
		$saoyue_ok = 0;
	}
}else{
	$saoyue_ok = 0;
}
include $this->template('saoyue/list');

